package cn.tedu.sp04.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 本类用于
 *
 * @author SJXQ
 * @version 2022/1/14 16:52
 */
@RestController
@CrossOrigin
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/{orderId}")
    public JsonResult<Order> getOrderById(@PathVariable("orderId") String OrderId){
        log.info("获取订单请求:orderId({})",OrderId);
        final Order order = orderService.getOrder(OrderId);
        return JsonResult.build().code(JsonResult.SUCCESS).msg("订单获取成功!").data(order);
    }
    @GetMapping("/add")
    public JsonResult add() {
        Order order = new Order();
        order.setId("u3jofd92jkfdss");
        order.setUser(new User(8,null,null));
        order.setItems(Arrays.asList(new Item[]{
                new Item(1,"商品1",1),
                new Item(1,"商品2",2),
                new Item(1,"商品3",3),
                new Item(1,"商品4",4),
                new Item(1,"商品5",5),
                new Item(1,"商品6",6),
                new Item(1,"商品7",7)
        }));
        log.info("新增订单请求:{}",order);
        orderService.addOrder(order);
        return JsonResult.build().code(JsonResult.SUCCESS).msg(String.format("新增订单:%s 成功", order));
    }
    @GetMapping("/favicon.ico")
    public void ico(){}
}
